package org.unt.parser;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.log4j.Logger;
import org.unt.genlogic.Files;

public class ParserPom {

	static Logger logger = Logger.getLogger(ParserPom.class.getName());

	static String sep = "\t";

	public static void main(String[] args) throws IOException {

		StringBuffer buf = new StringBuffer();

		List setToolbox = get("C:/DIGOJ/workspace1/PsPnrOutilJustifServer/pom.xml");
		List setPanier = get("C:/FleetBox/workspace2/PnrMetier/pom.xml");

		buf.append(toString(setToolbox));
		buf.append("\n\n");
		buf.append(toString(setPanier));

		buf.append("\n\nEn Toolbox, no en Panier\n");

		for (Iterator iterator = setToolbox.iterator(); iterator.hasNext();) {

			String dependencyToolbox = (String) iterator.next();

			if (!setPanier.contains(dependencyToolbox))
				buf.append(dependencyToolbox + "\n");
		}

		buf.append("\n\nEn Panier, no en Toolbox\n");

		for (Iterator iterator = setPanier.iterator(); iterator.hasNext();) {

			String dependencyPanier = (String) iterator.next();

			if (!setToolbox.contains(dependencyPanier))
				buf.append(dependencyPanier + "\n");
		}

		Files.writeToFile(buf.toString(), "C:/Speeder/workspace.12.1/Test/src/parser/out.txt");
	}

	static String toString(List list) {
		StringBuffer buf = new StringBuffer();
		for (Iterator iterator = list.iterator(); iterator.hasNext();) {
			String str = (String) iterator.next();
			buf.append(str + "\n");
		}
		return buf.toString();
	}

	static List get(String pom) throws IOException {

		List lines = Files.readFileAsList(pom);

		List list = new ArrayList();

		String groupId = null;
		String artifactId = null;
		String version = null;

		boolean contineWhileItNotAppeasrDependencies = true;
		for (Iterator iterator = lines.iterator(); iterator.hasNext();) {

			String line = ((String) iterator.next()).trim();

			if (contineWhileItNotAppeasrDependencies && line.indexOf("<dependencies>") > -1)
				continue;
			else
				contineWhileItNotAppeasrDependencies = false;

			if (line.indexOf("<exclusions>") > -1) {

				do {
					line = ((String) iterator.next()).trim();
				} while (line.indexOf("</exclusions>") < 0);

				continue;
			}

			// while (line.indexOf("<dependency>") < 0) {
			// if (iterator.hasNext()) {
			// line = ((String) iterator.next()).trim();
			// } else {
			// break;
			// }
			// }

			try {

				if (line.indexOf("<groupId>") > -1) {
					groupId = line.substring("<groupId>".length(), line.indexOf("</groupId>"));
				} else if (line.indexOf("<artifactId>") > -1) {
					artifactId = line.substring("<artifactId>".length(), line.indexOf("</artifactId>"));
				} else if (line.indexOf("<version>") > -1) {
					version = line.substring("<version>".length(), line.indexOf("</version>"));
				} else if (line.indexOf("</dependency>") > -1) {
					list.add(groupId + sep + artifactId + sep + version);
				}

			} catch (StringIndexOutOfBoundsException e) {
				logger.error("line '" + line + "'\npom '" + pom + "'", e);
			}
		}

		return list;
	}
}
